import { CurrencyPipe } from '@angular/common';
import { Component, OnInit, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import { STComponent, STColumn, STRequestOptions, STChange, STData } from '@delon/abc/st';
import { SFComponent, SFSchema, SFDateWidgetSchema } from '@delon/form';
import { SearchDrawerService } from '@shared';
import { NzModalService } from 'ng-zorro-antd/modal';
import { BasicTableComponent } from 'src/app/routes/commom';
import { FreightAccountService } from '../../services/freight-account.service';

@Component({
  selector: 'app-payable-order',
  templateUrl: './payable-order.component.html',
  styleUrls: ['../../../commom/less/commom-table.less', '../../../commom/less/common-table-bar.less']
})
export class PayableOrderComponent extends BasicTableComponent implements OnInit {
  columns: STColumn[] = this.initST();
  schema: SFSchema = this.initSF();

  selectedRows: any[] = [];
  info: any = {};
  constructor(public service: FreightAccountService, private router: Router, public searchDrawerService: SearchDrawerService) {
    super(searchDrawerService);
  }

  ngOnInit(): void {}

  search() {
    this.st?.load(1);
  }

  loadInfo() {
    this.service.request(this.service.$api_get_fico_ph_sum).subscribe(res => {
      if (res) {
        this.info = res;
      }
    });
  }

  beforeReq = (requestOptions: STRequestOptions) => {
    if (this.sf) {
      Object.assign(requestOptions.body, {
        ...this.sf?.value
      });
      if (this.sf?.value?.createTime) {
        Object.assign(requestOptions.body, {
          createTime: {
            start: this.sf?.value.createTime?.[0] || '',
            end: this.sf?.value.createTime?.[1] || ''
          }
        });
      }
      if (this.sf?.value?.phxdate) {
        Object.assign(requestOptions.body, {
          phxdate: {
            start: this.sf?.value.phxdate?.[0] || '',
            end: this.sf?.value.phxdate?.[1] || ''
          }
        });
      }
    }
    this.loadInfo();
    return requestOptions;
  };

  stChange(e: STChange): void {
    switch (e.type) {
      case 'checkbox':
        this.selectedRows = e.checkbox!;
        break;
    }
  }

  exportList() {
    this.service.exportStart(
      {
        ...this.sf?.value,
        createTime: {
          start: this.sf?.value.createTime?.[0] || '',
          end: this.sf?.value.createTime?.[1] || ''
        },
        phxdate: {
          start: this.sf?.value.phxdate?.[0] || '',
          end: this.sf?.value.phxdate?.[1] || ''
        },
        pageSize: -1
      },
      this.service.$api_get_fico_ph_page_export
    );
  }

  private initSF(): SFSchema {
    return {
      properties: {
        expand: {
          type: 'boolean',
          ui: {
            hidden: true
          }
        },
        phxcode: {
          type: 'string',
          title: '核销单号',
          ui: {
            placeholder: '请输入'
          }
        },
        billHCode: {
          type: 'string',
          title: '订单号',
          ui: {
            placeholder: '请输入'
          }
        },
        waybillHCode: {
          type: 'string',
          title: '运单号',
          ui: {
            placeholder: '请输入'
          }
        },
        ltdId: {
          type: 'string',
          title: '网络货运人',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            allowClear: true,
            asyncData: () => this.service.getNetworkFreightForwarder()
          },
          default: ''
        },
        bankreceipt: {
          type: 'string',
          title: '银行水单',
          ui: {
            placeholder: '请输入'
          }
        },
        brmtype: {
          type: 'string',
          title: '付款类型',
          enum: [{ value: '1', label: '费用款项' }],
          ui: {
            widget: 'select',
            placeholder: '请选择'
          }
        },
        cno: {
          type: 'string',
          title: '结算客户',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            allowClear: true,
            asyncData: () => this.service.getCloseAccount()
          }
        },
        driver2IdName: {
          type: 'string',
          title: '收款人',
          ui: {
            widget: 'select',
            placeholder: '请选择'
          }
        },
        sts: {
          type: 'string',
          title: '核销状态',
          default: '',
          ui: {
            widget: 'dict-select',
            containsAllLable: true,
            params: { dictKey: 'write:off:status' },
            containAllLable: true
          }
        },
        phxdate: {
          title: '核销日期',
          type: 'string',
          ui: {
            widget: 'sl-from-to-search',
            format: 'yyyy-MM-dd'
          } as SFDateWidgetSchema
        },
        createTime: {
          title: '创建时间',
          type: 'string',
          ui: {
            widget: 'sl-from-to-search',
            format: 'yyyy-MM-dd'
          } as SFDateWidgetSchema
        },
        feeHCode: {
          type: 'string',
          title: '费用号',
          ui: {
            placeholder: '请输入'
          }
        },
        remarks: {
          type: 'string',
          title: '核销备注',
          ui: {
            autocomplete: 'off',
            placeholder: '请选择'
          }
        }
      }
    };
  }

  private initST(): STColumn[] {
    return [
      { title: '', index: 'key', type: 'checkbox' },
      {
        title: '序号',
        className: 'text-center',
        width: '60px',
        format: (item: STData, col: STColumn, idx: number) => {
          return ((this.st.pi -1) * this.st.ps) + idx + 1 + ''
        } 
      },
      { title: '核销单号', index: 'phxcode', type: 'link', width: 160 },
      { title: '订单号', index: 'billHCode', width: 160 },
      { title: '运单号', index: 'waybillHCode', width: 160 },
      { title: '网络货运人', index: 'ltdName', width: 220 },
      { title: '核销日期', index: 'phxdate', type: 'date', width: 160 },
      { title: '付款账户', index: 'shipperaccount', width: 170 },
      { title: '收款账户', index: 'ltdaccountId', width: 190 },
      {
        title: '核销金额',
        index: 'phxmoney',
        width: 140,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.phxmoney }) }
      },
      {
        title: '应付金额',
        index: 'prmoney',
        width: 140,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.prmoney }) }
      },
      { title: '银行类型', index: 'banktype', type: 'enum', enum: { '1': '平安', '2': '浦发' }, width: 120 },
      { title: '付款类型', index: 'brmtype', type: 'enum', enum: { '1': '费用款项' }, width: 120 },
      { title: '收款人', index: 'driver2IdName', width: 120 },
      { title: '结算客户', index: 'cnoName', width: 180 },
      { title: '银行水单', index: 'bankreceipt', width: 190 },
      { title: '创建时间', index: 'createTime', width: 180 },
      // { title: '创建人', index: 'createUserIdLabel', width: 120 },
      { title: '核销状态', index: 'stsLabel', width: 120 },
      { title: '核销备注', index: 'remarks', width: 120 },
      {
        title: '操作',
        fixed: 'right',
        className: 'text-center',
        width: 120,
        buttons: [
          {
            text: '浏览',
            acl: { ability: ['FINANCIAL-PAYABLE-view'] },
            click: item =>
              this.router.navigate(['/financial-management/payable-order/detail/' + item.id], { queryParams: { billHId: item.id } })
          }
        ]
      }
    ];
  }
}
